﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Xml;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace QA.TFS.Rules.Validation
{

  [DisplayName("Fault Code Validation")]
  [Description("Validates if SOAP response contains some fault code")]
  public class FaultCodeValidationRule : ValidationRule
  {

    [DisplayName("Fault Code Validation")]
    [Description("Validates if SOAP response contains some fault code")]
    public override void Validate(object sender, ValidationEventArgs e)
    {
      bool validated = true;

      XmlNodeList faults = e.Response.XmlDocument.GetElementsByTagName("soap:Fault");

      if (faults.Count > 0)
        validated = false;

      foreach (XmlNode node in faults)
      {
        e.Message = String.Format("SOAP Error: ");

        foreach (XmlNode child in node.ChildNodes)
        {
          e.Message += String.Format("{0}= {1}, ", child.Name, child.InnerText);
        }

      }

      e.IsValid = validated;

    }

  }

}
